package com.example.utils;

import java.util.HashMap;
import java.util.Map;

import lombok.experimental.UtilityClass;

@UtilityClass
public class UserHelper {
    private static final ThreadLocal<Map<String, String>> THREAD_LOCAL = new ThreadLocal<>();
    private static final String OPEN_ID = "open_id";
    private static final String SESSION_KEY = "session_key";

    public void set(String key, String value) {
        Map<String, String> map = THREAD_LOCAL.get();
        if (map != null) {
            map.put(key, value);
        } else {
            Map<String, String> initMap = new HashMap<>();
            initMap.put(key, value);
            THREAD_LOCAL.set(initMap);
        }
    }

    public String get(String key) {
        Map<String, String> map = THREAD_LOCAL.get();
        return map.get(key);
    }

    public void setOpenId(String openId) {
        set(OPEN_ID, openId);
    }

    public String getOpenId() {
        return get(OPEN_ID);
    }

    public void setSessionKey(String sessionKey) {
        set(SESSION_KEY, sessionKey);
    }

    public String getSessionKey() {
        return get(SESSION_KEY);
    }

    public void remove() {
        THREAD_LOCAL.remove();
    }
}